var pageNumber = 1;
var queryStr = "";
var queryStrGlob = "";
$(function() {
	userShow();
});
/**
 * 设置值
 * 
 * @param queryStr
 *            检索字段
 * @param type
 *            类型
 * @param lableId
 *            标签
 * @param pageNum
 *            页码
 */
function valueSet(queryStr, pageNum) {
	$("#queryStr").val(queryStr);
	$("#pageNo").val(pageNum);
}
/**
 * 页码改变
 * 
 * @param pageNum
 */
function pageChange(pageNum) {
	valueSet(queryStrGlob, pageNum);
	userShow();
}
function query() {

}
function userShow() {
	var content = "";
	$
			.post(
					"getUserList",
					$("#valueForm").serialize(),
					function(responseResult) {
						// alert(responseResult.userList.length);
						if (responseResult.userList.length > 0) {
							$(responseResult.userList)
									.each(
											function(i) {
												var role = "";
												if (this.role == 0) {
													role = "普通教师";
												} else if (this.role == 1) {
													role = "院级领导";
												} else if (this.role == 2) {
													role = "管理员";
												}

												content += "<tr><td><input type='checkbox' id='"
														+ this.employeenumber
														+ "'/>"
														+ "</td><td>"
														+ (i + 1)
														+ "</td><td>"
														+ this.employeenumber
														+ "</td><td>"
														+ this.name
														+ "</td><td>"
														+ this.phone
														+ "</td><td>"
														+ this.email
														+ "</td><td>"
														+ this.part
														+ "</td><td>"
														+ this.office
														+ "</td><td>"
														+ this.address
														+ "</td><td>"
														+ role
														+ "</td></tr>";
											});
							$('#userList').html(content);
							pageShow(pageNumber,
									responseResult.responseParam.totalNum, '');
						} else {
							$('#userList').append("无相关数据！");
						}
					});
}

function pageShow(curPage, totalPage, pageFlag) {
	var ta = "";
	var lastPage = curPage - 1, nextPage = curPage + 1;
	if (lastPage < 1) {
		lastPage = 1;
	}
	if (nextPage > totalPage) {
		nextPage = totalPage;
	}
	ta += "<li><a href='javascript:pageChange(" + lastPage
			+ ")'>&laquo;</a></li>";
	if (curPage > 2 && curPage < parseInt(parseInt(totalPage) - 2)) {
		for (var j = parseInt(parseInt(curPage) - 2); j <= totalPage
				&& j <= parseInt(parseInt(curPage) + 2); j++) {
			if (j == curPage) {
				ta += "<li class='am-active'><a href='javascript:;'>" + j
						+ "</a></li>";
			} else {
				ta += "<li><a href='javascript:pageChange(" + j + ");'>" + j
						+ "</a></li>";
			}
		}
	} else if (curPage <= 2 && totalPage >= 5) {
		for (var j = 1; j <= 5; j++) {
			if (j == curPage) {
				ta += "<li class='am-active'><a href='javascript:;'>" + j
						+ "</a></li>";
			} else {
				ta += "<li><a href='javascript:pageChange(" + j + ");'>" + j
						+ "</a></li>";
			}
		}
	} else if (totalPage < 5) {
		for (var j = 1; j <= totalPage; j++) {
			if (j == curPage) {
				ta += "<li class='am-active'><a href='javascript:;'>" + j
						+ "</a></li>";
			} else {
				ta += "<li><a href='javascript:pageChange(" + j + ");'>" + j
						+ "</a></li>";
			}
		}
	} else {
		for (var j = parseInt(parseInt(totalPage) - 4); j <= totalPage; j++) {
			if (j == curPage) {
				ta += "<li class='am-active'><a href='javascript:;'>" + j
						+ "</a></li>";
			} else {
				ta += "<li><a href='javascript:pageChange(" + j + ");'>" + j
						+ "</a></li>";
			}
		}
	}
	ta += "<li><a href='javascript:pageChange(" + nextPage
			+ ");'>&raquo;</a></li>";
	$("#pagebody").html(ta);
}

// 实现全选的js代码
function sellectAll() {
	var input = document.getElementsByTagName("input");
	var notSelectCount = 0;
	var inputCount = input.length;
	for (var i = 0; i < inputCount; i++) {
		if (input[i].type == "checkbox") {
			if (input[i].checked == false) {
				notSelectCount++;
			}
		}
	}
	if (notSelectCount == 1) {
		for (var i = 0; i < inputCount; i++) {
			if (input[i].type == "checkbox") {
				input[i].checked = false;
			}
		}
	} else {
		for (var i = 0; i < inputCount; i++) {
			if (input[i].type == "checkbox") {
				input[i].checked = true;
			}
		}
	}
}
/**
 * 密码重置
 */
function passReset() {
//	var values = valueGetFromCheckbox();
	var pass = $("#adminPass").val().trim();
	if(pass == ""){
		alert("请输入管理员密码。");
		return;
	}
//	$("#")
	$.post("resetPass", $("#checkpass").serialize(), function(responseResult) {
			alert(responseResult.responseParam.message);
	});
}

/**
 * 修改权限
 */
function powerReset() {
	$.post("changeUser", $("#selectpower").serialize(), function(responseResult) {
		alert(responseResult.responseParam.message);
		location.reload();
	});
}

/**
 * 获取多选框的id组合
 * 
 * @returns {String}
 */
function valueGetFromCheckbox() {
	var input = document.getElementsByTagName("input");
	var inputCount = input.length;
	var values = "";
	for (var i = 0; i < inputCount; i++) {
		if (input[i].type == "checkbox" && input[i].checked) {
			if (values == "") {
				values += input[i].id;
			} else
				values += "," + input[i].id;
		}
	}
	return values;
}

/**
 * 修改密码页显示
 */
function showcheckpass() {
	var values = valueGetFromCheckbox();
	if (values.trim() == "") {
		alert("请选择用户。");
		return;
	}
	$(".resetInput").val(values);
	document.getElementById('shadow').style.display = '';
	document.getElementById('checkpass').style.display = '';
}
/**
 * 返回
 */
function returnback() {
	document.getElementById('checkpass').style.display = 'none';
	document.getElementById('shadow').style.display = 'none';
}
/**
 * 修改权限
 */
function showselectpower() {
	var values = valueGetFromCheckbox();
	if (values.trim() == "") {
		alert("请选择用户。");
		return;
	}
	$(".resetInput").val(values);
	document.getElementById('shadow').style.display = '';
	document.getElementById('selectpower').style.display = '';
}
/**
 * 返回
 */
function returnback2() {
	document.getElementById('selectpower').style.display = 'none';
	document.getElementById('shadow').style.display = 'none';
}